package main

import (
	"github.com/apache/rocketmq-client-go/v2/rlog"
	"github.com/spf13/pflag"
	"github.com/spf13/viper"
	"log/slog"
	"rocketmq-exporter-go/pkg/exporter"
	"rocketmq-exporter-go/pkg/logger"
	"rocketmq-exporter-go/pkg/util"
	// _ "net/http/pprof"
)

func init() {
	pflag.String("web.listen-address", ":9999", "Address to listen on for web interface and telemetry.")
	pflag.String("web.telemetry-path", "/metrics", "Path under which to expose metrics.")

	pflag.Bool("log.file.enable", false, "Log write to file enable.")
	pflag.String("log.file.path", "", "Path of log file.")

	pflag.StringSlice("rocketmq.nameserver.addr", []string{"127.0.0.1:9876"}, "Address of rocketmq nameserver. Support multi-address(using comma separate)")

	pflag.Int("workers", 100, "Number of workers")

	pflag.Parse()
	err := viper.BindPFlags(pflag.CommandLine)
	if err != nil {
		panic(err)
	}

	viper.SetConfigFile("config.yaml")
	_ = viper.ReadInConfig()
}

func main() {
	logger.SetLevel("INFO")
	listenAddress := viper.GetString("web.listen-address")
	metricsPath := viper.GetString("web.telemetry-path")

	logFileEnable := viper.GetBool("log.file.enable")
	logFilePath := viper.GetString("log.file.path")

	nameServerAddress := viper.GetStringSlice("rocketmq.nameserver.addr")

	workers := viper.GetInt("workers")

	// 打印配置
	slog.Info("Config ", "listenAddress", listenAddress,
		"metricsPath", metricsPath,
		"logFileEnable", logFileEnable,
		"logFilePath", logFilePath,
		"nameServerAddress", nameServerAddress,
		"workers", workers,
	)

	for i, address := range nameServerAddress {
		nameServerAddress[i] = util.ProcessAddress(address)
	}

	if logFileEnable {
		logger.SetOutputFile(logFilePath)
	}

	opts := &exporter.RocketmqOptions{
		NameServerAddress: nameServerAddress,
		Workers:           workers,
	}

	rlog.SetLogger(logger.NewRLog())
	slog.Info("Fly ", "opts", *opts)

	exporter.Fly(listenAddress, metricsPath, opts)
}
